﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using bdnt_pets_system.Tools;
using bdnt_pets_system_Entity;
using System.Globalization;

namespace bdnt_pets_system.MyUserControls
{
    public partial class MyRadioUserControl : UserControl
    {
        public List<SimpleObject<Tbl_DictInfo, bool>> ItemsSource
        {
            get
            {
                if (icLx.ItemsSource is List<SimpleObject<Tbl_DictInfo, bool>>)
                    return icLx.ItemsSource as List<SimpleObject<Tbl_DictInfo, bool>>;
                else return new List<SimpleObject<Tbl_DictInfo, bool>>();
            }
            set { icLx.ItemsSource = value; }
        }
        public SimpleObject<Tbl_DictInfo, bool> SelectedItem
        {
            get
            {
                if (ItemsSource.Count > 0)
                    return ItemsSource.FirstOrDefault(p => p.Item2 == true);
                else return null;
            }
        }
        public MyRadioUserControl()
        {
            InitializeComponent();
            icLx.ItemTemplate = SessionManager.IsCN_ZH ? LayoutRoot.Resources["dtLx"] as DataTemplate : LayoutRoot.Resources["en_dtLx"] as DataTemplate;
        }

        #region 数据的加载
        /// <summary>
        /// 加载数据字典
        /// </summary>
        /// <param name="pid">父级字典id</param>
        /// <param name="currentValue">当前选中字典dictValue值</param>
        public void LoadDict(int pid, string currentValue)
        {
            //菜单类型
            if (DictInfoManager.LoadDictsByPId(pid))
            {
                var cuos = DictInfoManager.CurrentDicts.Select(p => new SimpleObject<Tbl_DictInfo, bool>()
                {
                    Item1 = p,
                    Item2 = (currentValue == p.DictValue ? true : false)
                }).ToList();
                icLx.ItemsSource = cuos;
            }
            else
            {
                SystemServiceReference.SystemServiceClient sysClient = new SystemServiceReference.SystemServiceClient();
                sysClient.GetDictListByPidCompleted += new EventHandler<SystemServiceReference.GetDictListByPidCompletedEventArgs>(sysClient_GetDictListByPidCompleted);
                sysClient.GetDictListByPidAsync(pid, currentValue);
                sysClient.CloseAsync();
            }
        }

        void sysClient_GetDictListByPidCompleted(object sender, SystemServiceReference.GetDictListByPidCompletedEventArgs e)
        {
            var cos = e.Result.OrderBy(p => p.Sequence).ToList();
            DictInfoManager.AddToAllDicts(cos);
            var cuos = cos.Select(p => new SimpleObject<Tbl_DictInfo, bool>()
            {
                Item1 = p,
                Item2 = (e.UserState.ToString() == p.DictValue ? true : false)
            }).ToList();
            icLx.ItemsSource = cuos;
        }
        #endregion
    }
}
